package com.iwhalecloud.bss.kite.cucc.web.service.scene;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.inf.IInfBcmcServiceBus;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.CuccContextCustDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosCustomerDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.scene.CuccCustInfoDTO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.web.controller.scene.SceneController;
import com.iwhalecloud.bss.kite.cucc.web.service.IBcmcSceneService;
import com.iwhalecloud.bss.kite.cucc.web.service.ISceneService;
import com.iwhalecloud.bss.kite.cucc.web.service.accept.core.LocalAcceptEngine;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.vo.PageNavigationVO;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

/**
 * <Description> <br>
 * Bcmc流程场景服务类
 *
 * @author XXZ<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2020年12月10日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.web.service.scene <br>
 */
@Service("cuccbcmcSceneService")
public class BcmcSceneService implements IBcmcSceneService {

    @Autowired
    private IInfBcmcServiceBus infBcmcServiceBus;

    @Autowired
    private ISceneService sceneService;

    @Autowired
    private LocalAcceptEngine acceptEngine;

    @Override
    public DataResult<String> createOfferSceneByOrderDetailInfo(String orderId, String serviceOfferId, String custId,
        String custName, String staffId, String lanId, String regionId, String operType) {
        // 根据订单ID获取订单详情信息
        Map<String, Object> workOrderInfo = infBcmcServiceBus.getWorkOrderInfo(orderId);
        JSONObject orderData = (JSONObject) workOrderInfo.get("orderData");
        KitePosDTO kitePosDTO = JSONObject.toJavaObject(JSON.parseObject(JSON.toJSONString(orderData)),
            KitePosDTO.class);
        // 初始化上下文
        this.initContext(kitePosDTO.getCustomer());

        // 根据工单类型获取具体服务提供
        if (KiteStringUtils.isBlank(serviceOfferId)) {
            serviceOfferId = DcPublicCache.getPCode("20201130001", kitePosDTO.getOrder().getBizType());
        }

        // 创建场景实例
        String sceneInstId = sceneService.createOfferScene(serviceOfferId, operType, LocalKeyConsts.ASK_SOURCE_ORDER,
            KiteWebContext.getContext().getCustId(), KiteWebContext.getContext().getCust().getCustName(), staffId,
            lanId, regionId, kitePosDTO, null);

        IOperateSceneInstService operateSceneInstService = SpringUtil.getBean(IOperateSceneInstService.class);
        // 把客户信息设置场景扩展属性
        operateSceneInstService.putExtParameter(sceneInstId, "customer", kitePosDTO.getCustomer());

        return ErrorConsts.SUCCESS.getResult(sceneInstId);
    }

    private void initContext(KitePosCustomerDTO customer) {
        CuccCustInfoDTO cuccCustInfoDTO = KiteBeanUtils.transform(customer, CuccCustInfoDTO.class);
        CuccContextCustDTO contextCust = new CuccContextCustDTO();
        if (Objects.nonNull(cuccCustInfoDTO)) {
            KiteBeanUtils.copyProperties(cuccCustInfoDTO, contextCust);
            // 集团客户名称
            contextCust.setProvinceCode(cuccCustInfoDTO.getEparchyProvince());
            contextCust.setCustName(cuccCustInfoDTO.getName());
            contextCust.setLanId(cuccCustInfoDTO.getEparchyCity());
            contextCust.setRegionId(cuccCustInfoDTO.getEparchyArea());
            contextCust.setAttrs(cuccCustInfoDTO.getAttrs());
        }
        KiteWebContext.initContext(ContextUtil.getHttpSessoinId(), contextCust);
    }

    @Override
    public DataResult<SceneNewOfferSeneVO> getOrderSceneInfoView(String sceneInstId) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        // 获取场景中受理商品实例
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
        KiteOfferInst offerInst = null;
        if (Objects.nonNull(sceneInst)) {
            // 获取跟商品实例
            offerInst = querySceneInstService.getOfferInst(sceneInst.getCustId(), sceneInst.getRootInstId());
        }

        // 获取场景页面流对象
        PageNavigationVO pageNavigationVO = SpringUtil.getBean(SceneController.class)
            .getPageNavigation(sceneInst.getServiceOfferId(), OfferCache.get(offerInst.getOfferId()).getOfferType(),
                offerInst.getOfferId(), sceneInstId, null, null).getResultObject();
        if (pageNavigationVO == null) {
            ErrorConsts.BUSI_COMMON_ERROR.throwOut("未配置核查转定页面流程");
        }

        // 工单场景信息展示
        SceneNewOfferSeneVO sceneVO = (SceneNewOfferSeneVO) acceptEngine.previewScene(sceneInstId, KiteWebContext.getContext().getCustId(),
            pageNavigationVO.getTemplateId(), offerInst.getOfferId());

        return ErrorConsts.SUCCESS.getResult(sceneVO);
    }
}
